<template>
  <div>
    <h1 style="text-align: center">搜索影片</h1>
    <h2 style="text-align: center">影片名和ID二选一填写</h2>
    <el-form
      :model="ruleForm"
      status-icon
      ref="ruleForm"
      label-width="100px"
      :rules="rules"
      class="demo-ruleForm"
    >
      <el-form-item label="影片名" prop="name">
        <el-input
          type="name"
          v-model="ruleForm.name"
          autocomplete="off"
          placeholder="名字支持模糊查询哦"
        ></el-input>
      </el-form-item>
      <el-form-item label="影片id" prop="ID">
        <el-input
          type="ID"
          v-model="ruleForm.ID"
          autocomplete="off"
          placeholder="ID不支持模糊查询哦"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >查询</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { hhData } from "@/server/server";
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value && !this.ruleForm.ID) {
        return callback(new Error("影片名和ID不能都为空"));
      } else if (value && this.ruleForm.ID) {
        return callback(new Error("影片名和ID二选一填写"));
      } else {
        callback();
        console.log(1);
      }
    };
    var validateID = (rule, value, callback) => {
      if (!value && !this.ruleForm.name) {
        return callback(new Error("影片名和ID不能都为空"));
      } else if (value && this.ruleForm.name) {
        return callback(new Error("影片名和ID二选一填写"));
      } else {
        callback();
        console.log(1);
      }
    };
    return {
      ruleForm: {
        name: "",
        ID: "",
      },
      rules: {
        name: [{ validator: checkName, trigger: "submit" }],
        ID: [{ validator: validateID, trigger: "submit" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.name) {
            this.$router.push({
              name: "Video",
              query: {
                ...this.ruleForm,
              },
            });
          } else {
            this.$router.push({
              name: "EditVideo",
              query: {
                ...this.ruleForm,
              },
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style  scoped>
</style>